﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Data;
using Zxb.DayWork.Core;

namespace Zxb.DayWork.Logic
{
    public class Net
    {
        public static DataTable GetDTFromUrl(string url)
        {
            WebClient client = new WebClient();
            client.Proxy = null;
            Stream htmlStream = client.OpenRead(url);
            DataSet ds = new DataSet();
            ds.ReadXml(htmlStream);
            htmlStream.Close();
            return ds.Tables[0];
        }

        public static string GetStringFromUrl_Retry(string url)
        {
            WebClient client = new WebClient();
            client.Proxy = null;
            string result = null;
            Stream htmlStream = null;
            StreamReader sr = null;
            int retry = 5;
            for (int i = 0; i < retry; i++)
            {
                try
                {
                    htmlStream = client.OpenRead(url);
                    sr = new StreamReader(htmlStream);
                    result = sr.ReadToEnd();
                }
                catch (Exception ex)
                {
                    Logger.InfoLog.Warn(string.Format("读取400电话接口时错误：{0}, retry:{1}", ex.Message, i));
                }
                finally
                {
                    if (sr != null)
                    {
                        sr.Close();
                    }
                    if (htmlStream != null)
                    {
                        htmlStream.Close();
                    }
                }
                if (result != null)
                {
                    break;
                }
            }
            if (result != null)
            {
                //Logger.InfoLog.Info(string.Concat(procedureName, "执行成功"));
            }
            else
            {
                Logger.InfoLog.Error(string.Concat(url, "执行5次均出错"));
            }
            return result;
        }
    }
}
